﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gestures;
using WPFGestures.Gestures;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using nl.tno.ict.multitouch.wpfmultitouch.controls;
using nl.tno.ict.multitouch.wpfmultitouch;
using System.Windows.Media.Effects;


namespace WPFGestures.Gestures.WPF
{
    public class WPFPreview : Preview
    {

        //static Canvas canvas;
        public Path path;
        public MTImage image;
        // pad + texture
        // preview
        private Common.Point endPoint;

        // point where preview should be displayed

        static Canvas canvas;

        public string segmentName;


        public static void setCanvas(Canvas c)
        {
            canvas = c;
        }


        public override void setTranslation(float x, float y)
        {
            
        }
        



        public override void draw()
        {

        }


        // static int colorCounter = 0;

        public WPFPreview()
        {
            if (canvas == null)
            {
                throw new Exception("Cannot make WPFGesture, Canvas not set.");
            }

            //color = Color.FromRgb(0, 255, 0);
            //Brush b = Brushes.Black.Clone();
            //b.Opacity = 0.5;
            //b.col

            path = new Path();
            path.Stroke = Brushes.Red;
            path.Opacity = 0.8;
            path.StrokeThickness = 15;
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 125);
            path.Fill = mySolidColorBrush;


            //BitmapImage bmp = new BitmapImage(new Uri(path));
            //bmp.beg
            //Image i = new Image();
            //Image.ur
        }

        public override void delete()
        {
            canvas.Children.Remove(path);
            canvas.Children.Remove(image);
        }

        public void load(Candidate candidate, Common.Point position, string pathTexture, string imageSource, float relativeWidth, float relativeHeight)
        {
            segmentName = candidate.name;
            List<Common.Point> points = candidate.originalPoints;
           // candidate.originalPoints

            //WidenedPath wp;
            //StreamGeometry sg = new StreamGeometry();

            GeometryGroup myGeometryGroup = new GeometryGroup();

            PathGeometry pg = new PathGeometry();            
            PathFigure pf = new PathFigure();
            pf.StartPoint = new Point((points[0].x) * 1000 + position.x, position.y - (float)(points[0].y) * 1000);
            pf.IsFilled = false;
            //pf.st
            //pg.

            for (int i = 1; i < points.Count ; ++i)
            {
                
                float p1x = (float)(points[i].x) * 1000 + position.x;
                float p1y = position.y - (float)(points[i].y) * 1000 ;

                LineSegment ls = new LineSegment();//new Point((double)p1x, (double)p1y));
                ls.IsSmoothJoin = true;
                ls.IsStroked = true;
                
                ls.Point = new Point((double) p1x, (double)p1y);

                
                pf.Segments.Add(ls);

                pf.IsClosed = false;
            }

            pg.Figures.Add(pf);
            myGeometryGroup.Children.Add(pg);
            
            //Console.WriteLine("is = " + imageSource);
            //imageSource = "C:\\Documents and Settings\\cleveringawp\\Desktop\\MtSlideTest\\MtSlideTest\\bin\\Debug\\mirror.png";
            //myGeometryGroup.Transform.
            //path.Opacity = 0;
            path.Data = myGeometryGroup;

            path.Effect = new DropShadowEffect() { Color = Colors.White, ShadowDepth = 0, Opacity = 0.75 };

            canvas.Children.Add(path);

            //path.RenderTransform = new TranslateTransform(100, 0);
            //image.Opacity = path.Opacity = 1;
            image = new MTImage();
            
            image.Width = 50;
            
            BitmapImage myBitmapImage = new BitmapImage();
            myBitmapImage.BeginInit();
            imageSource = Environment.CurrentDirectory + @"\images\" + imageSource;
            myBitmapImage.UriSource = new Uri(imageSource);
            myBitmapImage.DecodePixelWidth = 50;
            myBitmapImage.EndInit();

            image.Source = myBitmapImage;

            // calculate end point of preview path
            float endx = (float)(points[points.Count - 1].x) * 1000 + position.x + candidate.relativeWidth;
            float endy = position.y - (float)(points[points.Count - 1].y) * 1000 + candidate.relativeHeight;
            TransformGroup tg = new TransformGroup();
            tg.Children.Add(new TranslateTransform(endx, endy));
            image.RenderTransform = tg;
            canvas.Children.Add(image);

            image.Opacity = 0;
            path.Opacity = 0;
            //image.SetValue(Image.OpacityProperty, 0);
            //DoubleAnimation da = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1)));
            //image.BeginAnimation(Image.OpacityProperty, da);

            //DoubleAnimation da2 = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1)));
            //path.BeginAnimation(Path.OpacityProperty, da2);

           

            

            //path.BeginAnimation(Path.OpacityProperty, da);

            //myImage.

            //image.TouchEnabled = true;
            MTProcessing processing = MTProcessing.GetInstance();
            TouchManager tmd = processing.getTouchManager(image) as TouchManager;
            tmd.NewTouch += new Events.TouchEventHandler(tmd_NewTouch);
            //image.tou
        }

        void tmd_NewTouch(object sender, Events.TouchEventArgs obj)
        {
            iClicked = true;
            //Console.WriteLine("dfsdf");
            //throw new NotImplementedException();
        }

        bool iClicked = false;
        public bool imageClicked()
        {
            return iClicked;
        }


        public override void update()
        {
            // shift path texture etc.

            /*
                for (int j = 0; j < path.Length; ++j)
                {
                    path[j].TextureCoordinate.Y += 0.01f;
                    path[j].Color.A = 255;
                }
     */
        }

    }
}
